﻿/*********************************************************************************/
/**    Pattern Preset Color Extractor                                           **/
/**    Copyright (C) 2012  Anja Knackstedt                                      **/
/**                                                                             **/
/**    This program is free software: you can redistribute it and/or modify     **/
/**    it under the terms of the GNU General Public License as published by     **/
/**    the Free Software Foundation, either version 3 of the License, or        **/
/**    (at your option) any later version.                                      **/
/**                                                                             **/
/**    This program is distributed in the hope that it will be useful,          **/
/**    but WITHOUT ANY WARRANTY; without even the implied warranty of           **/
/**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            **/
/**    GNU General Public License for more details.                             **/
/**                                                                             **/
/**    You should have received a copy of the GNU General Public License        **/
/**    along with this program.  If not, see <http://www.gnu.org/licenses/>.    **/
/**                                                                             **/
/*********************************************************************************/

using System.IO;

namespace PatternPresetColorExtractor
{
    class PackageFileCreator
    {
        /// <summary>
        /// reads the pattern package bytes from the sims3pack file
        /// saves read bytes to a .package.tmp file
        /// </summary>
        /// <param name="patternPackage"></param>
        public void createTmpPackageFile(PatternPackage patternPackage)
        {
            BinaryReader binaryReader = new BinaryReader(new FileStream(patternPackage.Sims3PackPath, FileMode.Open, FileAccess.Read));
            binaryReader.BaseStream.Position = patternPackage.BinaryStartPosition + patternPackage.Offset;

            byte[] packageBytes = binaryReader.ReadBytes(patternPackage.Length);

            FileInfo packageSims3PackFileInfo = new FileInfo(patternPackage.Sims3PackPath);
            string fullname = packageSims3PackFileInfo.FullName;
            fullname = fullname.Replace(packageSims3PackFileInfo.Extension, "_tmp.package");

            writeBytesToFile(fullname, packageBytes);
            patternPackage.TmpPackagePath = fullname;
        }

        private void writeBytesToFile(string fileName, byte[] bytes)
        {

            using (FileStream
            fileStream = new FileStream(fileName, FileMode.Create))
            {
                // Write the data to the file, byte by byte.
                for (int i = 0; i < bytes.Length; i++)
                {
                    fileStream.WriteByte(bytes[i]);
                }

            }
        }
    }
}
